<template>
  <div>
    <p>用户类型饼状图</p>
    <div id="statistics" :style="{width: '100%', height: '300px'}" />
  </div>
</template>
<script>
import Vue from "vue";
import echarts from "echarts";
Vue.prototype.$echarts = echarts;
export default {
  name: "Hello",
  props: ["message1"],
  data() {
    return {};
  },
  watch: {
    message1: function(newVal, oldVal) {
      let arr = [];
      newVal.map(function(value, index) {
        arr.push(value.name)
      });
      this.drawLine(newVal, arr);
    }
  },
  methods: {
    drawLine(a, r) {
      // 基于准备好的dom，初始化echarts实例
      const myChart = this.$echarts.init(document.getElementById("statistics"));
      // 绘制图表
      myChart.setOption({
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b}: {c} ({d}%)"
        },
        legend: {
          orient: "vertical",
          x: "left",
          data: r
        },
        series: [
          {
            name: "访问来源",
            type: "pie",
            radius: ["50%", "70%"],
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: false,
                position: "center"
              },
              emphasis: {
                show: true,
                textStyle: {
                  fontSize: "30",
                  fontWeight: "bold"
                }
              }
            },
            labelLine: {
              normal: {
                show: false
              }
            },
            data: a
          }
        ]
      });
    }
  }
};
</script>
